<?php
class User_UserController extends Promotor_Controller_Action_Scaffold {
	public $skin = array(
		// forsuje domyslny template i style na admin
		'template' => 'admin',
		'style' => 'default',
		// layout
		'layout' => 'admin_user',
		// rozszerzam konfiguracje
		'config' => array(
			'filename' => 'backend_config.ini'
		)
	);

	public $acl = array(
		'list' => 'list',
		'add' => 'create',
		'edit' => 'update',
		'delete' => 'admin_delete'
	);
	
	/**
	 * @see Promotor_Controller_Action_Scaffold specialization
	 */
	protected $_modelClass = 'User_Model_User';
	protected $_formAddClass = 'User_Form_UserAdd';
	protected $_formEditClass = 'User_Form_UserEdit';
	protected $_formRemoveClass = 'User_Form_UserRemove';
	
	/**
	 * @return void
	 */
	public function indexAction() {
		$this->_forward('list');
	}
	
	/**
	 * @return void
	 */
	public function listAction() {
		$model = new User_Model_User();
		$table = $model->getDbTable();

		$rq = $this->getRequest();
		if ($rq->isPost()) {
			switch ($rq->getPost('action_type')) {
				case 'update':
					if (null !== $rq->getPost('editable')) {
						if ($this->_helper->acl->isAllowed('update')) {
							$data = $rq->getPost('editable');
							$model->editableUpdate($data);
							$this->_helper->flashMessenger($model->getStatus());
						}
					}
					break;
				case 'delete':
					if (null !== $rq->getPost('action_checked')) {
						if ($this->_helper->acl->isAllowed('admin_delete')) {
							$data = $rq->getPost('action_checked');
							$model->editableDelete($data);
							$this->_helper->flashMessenger($model->getStatus());
						}
					}
					break;
			}			
		}

		// setup data grid
		$config = $this->_helper->config('user.xml');
		$grid = KontorX_DataGrid::factory($table, $config->grid);
		$grid->setValues($this->_getAllParams());

		$this->_setupDataGridPaginator($grid);

		$this->view->grid = $grid;
	}
	
	public function thumbAction() {
        // wylaczenie renderowania widoku
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        // pobieranie konfiguracji
        $config = $this->_helper->config('user.xml');

        $resizerConfig = $config->resizer->toArray();
        $resizerConfig['dirname'] = rtrim(PUBLIC_PATHNAME,'/\\') . '/' . $resizerConfig['dirname'];

        $type = $this->_getParam('type');
        $filename = $this->_getParam('file');
        $thumbPathname = $type . DIRECTORY_SEPARATOR . $filename;

        try {
            // resizing
            require_once 'KontorX/Image/Resizer.php';
            $resizer = new KontorX_Image_Resizer($resizerConfig);
            $resizer->setFilename($filename);
            $resizer->setMultiType($type);
            $image = $resizer->resize();

            // display
            $image->display();

            // save
            require_once 'KontorX/File/Write.php';
            $write = new KontorX_File_Write($options);
            $write->setBasedir($resizer->getDirname());
            $write->setForce(true);
            $write->write($thumbPathname, $image->toString());

            return;
        } catch (KontorX_Image_Exception $e) {
            Zend_Registry::get('logger')
            ->log($e->getMessage() . "\n" . $e->getTraceAsString(), Zend_Log::WARN);
        } catch (KontorX_File_Exception $e) {
            Zend_Registry::get('logger')
            ->log($e->getMessage() . "\n" . $e->getTraceAsString(), Zend_Log::WARN);
        }

        // wyswietlanie domyslnej grafiki
        $this->_helper->redirector->goToUrlAndExit(
            $this->getFrontController()->getBaseUrl() . $config->defaultImage
        );
    }
}